package control;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.User;
import dbconn.DBConnect;

/***********
 * 管理员控制类
 * 用户登录判断
 * 获取用户信息
 * 用户信息修改
 * 添加用户
 * 删除用户
 * 获取所有用户
 * @author Cyoung
 * date 05/12/2012
 *
 */

public class UserControl {
	public static boolean isUser(String username, String password) {
		DBConnect dbc = new DBConnect();
		String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
		dbc.prepareStatement(sql);
		try {
			dbc.setString(1, username);
			dbc.setString(2, password);
			ResultSet rs = dbc.executeQuery();
			if (rs.next()){
				return true;
			}
			else
				return false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbc.close();
		}
		return false;
	}
	
	public static int getUid(String username, String password) {
		DBConnect dbc = new DBConnect();
		String sql = "SELECT id FROM user WHERE username = ? AND password = ?";
		int uid=0;
		dbc.prepareStatement(sql);
		try {
			dbc.setString(1, username);
			dbc.setString(2, password);
			ResultSet rs = dbc.executeQuery();
			if (rs.next()){
				uid=rs.getInt("id");
			}
			return uid;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbc.close();
		}
		return uid;
	}
	
	
	public static User getUserformation(int id){
		DBConnect dbc = new DBConnect();
		String sql = "SELECT * FROM user WHERE id= ?";
		dbc.prepareStatement(sql);
		try {
			User user=new User();
			dbc.setInt(1,id);
			ResultSet rs = dbc.executeQuery();
			if(rs.next())
			{
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setRealname(rs.getString("realname"));
				user.setLastip(rs.getString("lastip"));
				user.setId(id);
				return user;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			dbc.close();
		}
		return null;
	}
	
	public static boolean updateUser(User user,int id){
		DBConnect dbc = new DBConnect();
		String sql = "UPDATE user SET username=?,password=?,realname=? WHERE id=?";
		dbc.prepareStatement(sql);
		try {
			dbc.setString(1,user.getUsername());
			dbc.setString(2,user.getPassword());
			dbc.setString(3, user.getRealname());
			dbc.setInt(4, id);
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			dbc.close();
		}
	}
	
	public static boolean insertUser(User user)
	{
		DBConnect dbc=new DBConnect();
		String sql="insert into user (username,password,realname) values (?,?,?)";
		dbc.prepareStatement(sql);
		try
		{
			dbc.setString(1,user.getUsername());
			dbc.setString(2,user.getPassword());
			dbc.setString(3,user.getRealname());
			dbc.executeUpdate();
			return true;
		}catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			dbc.close();
		}
	}
	
	public static boolean deleteUser(int id){
		DBConnect dbc = new DBConnect();
		String sql = "DELETE FROM user WHERE id=?";
		dbc.prepareStatement(sql);
		try {
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			dbc.close();
		}
	}
	
	public  static boolean updateIp(int uid) {
	     DBConnect dbc = new DBConnect();
	     String ip="";
	     try {
		ip = InetAddress.getLocalHost().toString().split("/")[1];
		String sql = "update user set lastip = '"+ip+"' where id='"+uid+"'";
		dbc.prepareStatement(sql);
		try {
		    dbc.executeUpdate();
		    return true;
		} catch (Exception e) {
		    // TODO: handle exception
		    e.printStackTrace();
		    return false;
		}
	     } catch (UnknownHostException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	     }finally{
		    dbc.close();
		}
	     return false;
	}
	
	public static List<User> getAllUsers() {
		List<User> list = new ArrayList<User>();
		String sql ="select * from user";
		DBConnect dbConnect=  new DBConnect();
		ResultSet rsResultSet = null;
		dbConnect.prepareStatement(sql);
		try {
			rsResultSet = dbConnect.executeQuery();
			while(rsResultSet.next()){
				User u = new User();
				u.setId(rsResultSet.getInt("id"));
				u.setPassword(rsResultSet.getString("password"));
				u.setUsername(rsResultSet.getString("username"));
				u.setRealname(rsResultSet.getString("realname"));
				list.add(u);
			}
			return list;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}finally{
			dbConnect.close();
		}
	}
	

}
